<template>
  <!-- <h1>hello</h1>-->
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import resize from '@/views/dashboard/mixins/resize'
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

import {DateOrder} from "@/api/order/order";

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      DateOrder().then(response => {
        let names=[];//日期
        let values=[];//当日订单消费总额
        console.log(response.data);
        for(let i=0;i<response.data.length;i++){
          const formatteredDate = formatDate(response.data[i].name);
          names.push(formatteredDate);
          values.push(response.data[i].number);
        }
        const chartData = names.map((name, index) => ({
          value: values[index],
          name: name
        }));
        this.chart.setOption({
          title:{
            text:"每日销售额统计",
            left:'center'
          },
          xAxis: {
            type: 'category',
            data: names
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: values,
              type: 'line',
              smooth: true
            }
          ]
        })
      })
      // 定义 formatDate 函数
      function formatDate(isoDateString) {
        const date = new Date(isoDateString);
        return `${date.getFullYear()}-${addZero(date.getMonth() + 1)}-${addZero(date.getDate())}`;
      }

// 辅助函数，用于在单个数字前添加零
      function addZero(num) {
        return num.toString().padStart(2, '0');
      }
    }
  }
}
</script>
<style>
</style>
